/*
 * @(#)LocalCollection.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.media;

import java.io.File;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <code>LocalCollection</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.3 $
 * @since 0.1
 */
public class LocalCollection extends MediaCollection {
    private static final Log log = LogFactory.getLog(LocalCollection.class);

    private File device;

    protected LocalCollection(String name, String source) throws IOException {
        super();
        this.name = name;
        this.device = new File(source);

        this.initialize();
    }

    private void initialize() throws IOException {
        if (this.isDeviceActive()) {

            // loading up files is done in a seperate thread
            // new Thread() {
            // public void run() {
            // try {
            // find(device);
            // log.info("adding " + mediaFiles.size() + " songs");
            // PlayerManager.getInstance().setInitialized(true);
            // } catch (IOException ex) {
            // log.warn(ex.toString(), ex);
            // }
            // }
            // }.start();

            this.find(device);
            log.info("adding " + mediaFiles.size() + " songs");
        }
    }

    private boolean isDeviceActive() {
        if (device == null) {
            log.warn("Null device");
            return false;
        }

        if (!device.exists()) {
            log.warn("device doesn't exist : " + device);
            return false;
        }

        if (!device.isDirectory()) {
            log.warn("device is not a directory : " + device);
            return false;
        }

        if (!device.canRead()) {
            log.warn("device is not readable  : " + device);
            return false;
        }
        return true;
    }

    private synchronized void find(File root) throws IOException {
        File[] files = root.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                find(files[i]);
            }
            else {
                if (isMediaFile(files[i])) {
                    // @todo just supportng mp3 for now
                    Mp3File f = new Mp3File(files[i]);
                    this.mediaFiles.add(f);
                }
            }
        }
    }

    private boolean isMediaFile(File file) {
        return (file.getName().endsWith("mp3")
                || file.getName().endsWith("mp4") || file.getName().endsWith(
                "m4a"));
    }

    /**
     * @return Returns the device.
     */
    public synchronized File getDevice() {
        return this.device;
    }

    /**
     * @param device
     *            The device to set.
     */
    public synchronized void setDevice(File device) {
        this.device = device;
    }
}